package online.xybh.qa.controller;


import com.github.pagehelper.PageInfo;
import online.xybh.qa.model.Question;
import online.xybh.qa.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class IndexController {

    @Autowired
    private QuestionService questionService;


    @GetMapping(value = {"index", "/"})
    public String index(@RequestParam(value = "subject", defaultValue = "1") Integer subject,
                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                        @RequestParam(value = "size", defaultValue = "10") Integer size,
                        Model model) {
        PageInfo<Question> questionPageInfo;
        int sub = 0;
        switch (subject) {
            case 1:
                questionPageInfo = questionService.queryChinese(page, size);
                sub = 1;
                break;
            case 2:
                questionPageInfo = questionService.queryMath(page, size);
                sub = 2;
                break;
            case 3:
                questionPageInfo = questionService.queryEnglish(page, size);
                sub = 3;
                break;
            case 4:
                questionPageInfo = questionService.queryProgram(page, size);
                sub = 4;
                break;
            default:
                questionPageInfo = null;

        }
        model.addAttribute("subject", sub);
        model.addAttribute("questionList", questionPageInfo);
        model.addAttribute("backUrl", "/index");
        return "index";
    }
}
